const express=require('express');
const mysql=require('mysql');
const urlobj=require('url');
//引用自定义
//const commte=require('../../libs/commtle');
//链接数据库
var db=mysql.createPool({
    host:'localhost',
    user:'root',
    password:'',
    database:'leasedb'
});
//========================
//业务逻辑
module.exports = function() {
    var router = express.Router();

    //加载一级分类全部
    router.get("/updateTheSecondary.html",function(req,res,next){
        //判断是否登陆
        if(req.session['userId']==null||req.session['userId']=="")
        {
            res.status(400).send('<script>alert("请登录");window.location.href="login.html";</script>').end();
        }
        db.query("SELECT * FROM classa",function(err,data){
            if(err)
            {
                res.status(500).send('<script>alert("出错啦");</script>').end();
            }else
            {
                if(data.length==0)
                {
                    res.status(400).send('<script>alert("没有数据!");window.location.href="updateTheSecondary.html";</script>').end();
                }else {
                    req.Namecont=data;

                }
                next();
            }
        })
    });


    router.get("/updateTheSecondary.html",function(req,res,next){
        var classBId=+req.query.classBId;

        db.query("SELECT * FROM classb,classa WHERE classb.classAId=classa.classAId and classb.classBId="+classBId+"",function(err,data){
                    if(err)
                    {
                         res.status(500).send('<script>alert("出错啦");</script>').end();
                    }else
                    {
                        if(data.length==0)
                        {
                            res.status(400).send('<script>alert("没有数据!");window.location.reload();</script>').end();
                        }else {
                           req.secondclassAId=data;

                        }
                        next();
                    }
            })
    });


//修改
    router.use("/SecondLevel",function(req,res,next){
                 db.query("UPDATE classb SET classBName='"+ (req.body.classbname)+"',classAId="+(+req.body.classaID)+" WHERE classBId="+ (+req.body.ClassaClassBId)+"",function(err,data) {
                    if(err)
                    {
                        res.status(500).send('<script>alert("sql修改失败!");window.location.href="404.html";</script>').end()

                    }else
                    {
                        if(data.length=0)
                        {
                            res.status(300).send('<script>alert("修改失败!");window.location.href="updateTheSecondary.html";</script>').end()
                        }else
                        {
                            res.status(200).send('<script>alert("修改成功!");window.location.href="TheSecondary.html";</script>').end()
                        }
                        next();
                    }
                })
    });
    router.get('/updateTheSecondary.html',function(req,res){
         res.render("admin/updateTheSecondary.ejs",{cclassAconts:req.Namecont,secondclassAId:req.secondclassAId});
    });
    // router.get('/updateTheSecondary.html',function(req,res,next){
    //     res.render("admin/updateTheSecondary.ejs");
    // })
    return router;
}